import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Home/Home.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/Calendar',
        name: 'Calendar',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Home/Calendar.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/IndexAnchor',
        name: 'IndexAnchor',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Home/IndexAnchor.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/searchResult',
        name: 'searchResult',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Home/searchResult.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/orderSubmit',
        name: 'orderSubmit',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Home/orderSubmit.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/TicketingInstructions',
        name: 'TicketingInstructions',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Home/TicketingInstructions.vue'),
        meta: {
            requireAuth: false
        }
    },

    // 订单相关
    {
        path: '/myorder',
        name: 'myorder',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Myorder/myorder.vue'),
        meta: {
            requireAuth: false,
        }
    },
    {
        path: '/orderDetails',
        name: 'orderDetails',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Myorder/orderDetails.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/WaitToTRavel',
        name: 'WaitToTRavel',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Myorder/WaitToTRavel.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/Rebook',
        name: 'Rebook',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Myorder/Rebook.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/RebookOrderSubmit',
        name: 'RebookOrderSubmit',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Myorder/RebookOrderSubmit.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/BookingAgain',
        name: 'BookingAgain',
        component: () => import(/* webpackChunkName: "Article" */
            'views/Myorder/BookingAgain.vue'),
        meta: {
            requireAuth: false
        }
    },
    // 个人中心内容
    {
        path: '/user',
        name: 'user',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/user.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/userInfo',
        name: 'userInfo',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/userInfo.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/CommonInfo',
        name: 'CommonInfo',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/CommonInfo.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/addPassenger',
        name: 'addPassenger',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/addPassenger.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/editPassenger',
        name: 'editPassenger',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/editPassenger.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/Feedback',
        name: 'Feedback',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/Feedback.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/withdraw',
        name: 'withdraw',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/withdraw.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '/share',
        name: 'share',
        component: () => import(/* webpackChunkName: "Article" */
            'views/User/share.vue'),
        meta: {
            requireAuth: false
        }
    },
    {
        path: '*',
        name: '404',
        component: () => import(/* webpackChunkName: "404" */
            'views/404.vue'),
        meta: {
            requireAuth: true
        }
    },
];

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
});

router.selfaddRoutes = function (params) {
    router.matcher = new router().matcher;
    router.addRoutes(params);
}
// 路由守卫
router.beforeEach((to, from, next) => {
    // console.log(to);
    let getFlag = Boolean(localStorage.getItem("token"));
    if (!to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        // if (getFlag === true) {  // 通过vuex state获取当前的token是否存在
            if(to.name == 'home' ||
                to.name == 'myorder' ||
                to.name == 'user' ||
                to.name == 'Feedback' ||
                to.name == 'userInfo' ||
                to.name == 'CommonInfo' ||
                to.name == 'IndexAnchor' ||
                to.name == 'searchResult' ||
                to.name == 'orderSubmit' ||
                to.name == '404'
                ){
                window.document.body.style.backgroundColor = '#f7f7f7';
            }else {
                window.document.body.style.backgroundColor = '';
            }
            next();
        // }
        // else {
        //     console.log('应该路由拦截了');
        //     next({
        //         path: '/home',
        //         query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        //     })
        // }
    }
    else {
        next();
    }
});

export default router
